
***********CALCULATE MONTHLY S&P CONVEXITY*************
est clear

use "$jfqa_rep/CRSP_Daily_Index.dta", clear

qui{
drop if missing(spindx) 

sort year month day

gen monthyear=ym(year, month)

*assign first and last observation of month
gen prc_first=spindx if month~=month[_n-1]
egen min_prc=min(prc_first), by(year month)
replace prc_first=min_prc if missing(prc_first)
drop min_prc

gen prc_last=spindx if month~=month[_n+1]
egen min_prc=min(prc_last), by(year month)
replace prc_last=min_prc if missing(prc_last)
drop min_prc

sort year month
egen avgprc=mean(spindx), by(year month)
egen stdprc=sd(spindx), by(year month)
gen convexity=((prc_first+prc_last)/2-avgprc)/((prc_first+prc_last)/2)

drop if monthyear==monthyear[_n+1]

keep month year convexity
}

save "$jfqa_rep/TIA9_SP_Convexity.dta", replace


******PRODUCE TABLE IA9_A ESTIMATES*****
import delimited "$jfqa_rep/Goyal_Excel_Monthly.csv", clear

qui{
rename index sp_index 
rename d12 sp_div
rename e12 sp_earn

drop if missing(sp_index)

destring sp_index, force replace

merge 1:1 year month using "$jfqa_rep/CRSP_Monthly_Index.dta"
keep if _merge==3
drop _merge

gen monthyear=ym(year, month)
tsset monthyear
sort monthyear

gen log_prem=(crsp_spvw-log(1+rf))*100 

merge 1:1 year month using "$jfqa_rep/TIA9_SP_Convexity.dta"
keep if _merge==3
drop _merge
sort monthyear

rm "$jfqa_rep/TIA9_SP_Convexity.dta"

order year month monthyear log_prem

drop if year<1963
drop if year==1963 & month<6

egen convexity_std=std(convexity)

gen log_prem_3=(log_prem+F.log_prem+F2.log_prem)/3
gen fut_log_prem_3=(F3.log_prem+F4.log_prem+F5.log_prem)/3
gen fut3_log_prem_3=(F6.log_prem+F7.log_prem+F8.log_prem)/3
gen fut6_log_prem_3=(F9.log_prem+F10.log_prem+F11.log_prem)/3
}

newey log_prem_3 L.convexity_std , lag(6)
newey fut_log_prem_3 L.convexity_std , lag(6)
newey fut3_log_prem_3 L.convexity_std , lag(6)
newey fut6_log_prem_3 L.convexity_std , lag(6)

reg log_prem_3 L.convexity_std
reg fut_log_prem_3 L.convexity_std 
reg fut3_log_prem_3 L.convexity_std 
reg fut6_log_prem_3 L.convexity_std
